﻿<?php

class DoApp extends BaseApp
{
    function order()
    {
        if(!isset($_POST['keyword']))
		{
		$_POST['keyword'] = '';
		}
        if(!isset($_POST['type']))
		{
		$_POST['type'] = '';
		}
        if(!isset($_POST['status']))
		{
		$_POST['status'] = '';
		}
        if(!isset($_GET['keyword']))
		{
		$_GET['keyword'] = '';
		}
        if(!isset($_GET['type']))
		{
		$_GET['type'] = '';
		}
        if(!isset($_GET['status']))
		{
		$_GET['status'] = '';
		}
		$keyword=$_POST['keyword']==''?$_GET['keyword']:$_POST['keyword'];
		$searchtype=$_POST['type']==''?$_GET['type']:$_POST['type'];
		$status=$_POST['status']==''?$_GET['status']:$_POST['status'];
		$TB_leaveword = &$this->_model('do');  				
		$data = $TB_leaveword->show($keyword,$searchtype,$status);	
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);				
        $this->assign('searchtype', $searchtype);		
        $this->assign('status', $status);	
        $this->assign('keyword', $keyword);	
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
        $this->assign('site_url',SITE_URL);
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('do_order_list.view.html');
    }

    function usermoney()
    {
        if(!isset($_POST['keyword']))
		{
		$_POST['keyword'] = '';
		}
        if(!isset($_POST['type']))
		{
		$_POST['type'] = '';
		}
        if(!isset($_POST['status']))
		{
		$_POST['status'] = '';
		}
        if(!isset($_GET['keyword']))
		{
		$_GET['keyword'] = '';
		}
        if(!isset($_GET['type']))
		{
		$_GET['type'] = '';
		}
        if(!isset($_GET['status']))
		{
		$_GET['status'] = '';
		}
		$keyword=$_POST['keyword']==''?$_GET['keyword']:$_POST['keyword'];
		$searchtype=$_POST['type']==''?$_GET['type']:$_POST['type'];
		$status=$_POST['status']==''?$_GET['status']:$_POST['status'];
		$TB_leaveword = &$this->_model('do');  				
		$data = $TB_leaveword->usermoney($keyword,$searchtype,$status);	
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);				
        $this->assign('searchtype', $searchtype);		
        $this->assign('status', $status);	
        $this->assign('keyword', $keyword);	
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
        $this->assign('site_url',SITE_URL);
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('do_usermoney_list.view.html');
    }

	function ordercontent()
	{
		$id=$_GET['id'];		 	
		$TB_order = &$this->_model('do');  				
		$order = $TB_order->ordercontent($id);
        $product = $this->buyidstr_to_product_msg($order['productmsg']);
		$moduledata	= array();	
	    $tempmoduledata =  & $GLOBALS['SCup']->_get_module_list_data();
		foreach($tempmoduledata as $key => $value)
		{
            $moduledata[$value['id']] = $key;
		}
        unset($tempmoduledata);			
        $this->assign('moduledata',  $moduledata);		
        $this->assign('product', $product);
        $this->assign('order', $order);
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('do_order_content.view.html');
	}

    function buyidstr_to_product_msg($buyidstr,$ischeck=0)
	{
		$return_data = array();
		$productdata = array();
		$productcheck = array();
		$shopmoney = 0 ;
		$totalmoney = 0 ;		
		$numcheck = true;		
		$check = true ;		
		$buyarray = $this->buyidstr_to_array($buyidstr);				
        if(!empty($buyarray))
		{
		   $sql_in_str = array();
		   foreach($buyarray as $key=>$value)
		   {
		       if(!isset($sql_in_str[$value['0']]))$sql_in_str[$value['0']]='';
		       $sql_in_str[$value['0']].=$value['2'].',';
		   }
	       $db = & get_db();		   
		   foreach($sql_in_str as $key =>$value)
		   {
		      $fids= substr($value,0,-1);
			  $productdata[$key] = $db->get_all("SELECT * FROM ".DB_PREFIX."content_".$key." WHERE id IN ($fids)");			  
		   }   		   		  
		}
		if($ischeck&&!empty($buyarray)&&!empty($productdata))
		{
		    foreach($productdata as $key => $value)
			{
			    foreach($value as $key1 => $value1)
				{
				   $keystr = $key .'_'. $value1['cat_id'] .'_'. $value1['id'];
				   $productcheck[$keystr]['check']=1;
				   $productcheck[$keystr]['shoptype']=$value1['shoptype'];
				   $productcheck[$keystr]['numcheck']=$value1['shopnum'];
				   $shopmoney+=$value1['shopmoney'];
		           $totalmoney+=$value1['nowprice']*$buyarray[$keystr]['3'];
				}				
			}					
		    foreach($buyarray as $key => $value)
			{
				if(!isset($productcheck[$key]))
				{
				$productcheck[$key]['check']=0;
				$productcheck[$key]['name']='';
				$productcheck[$key]['numcheck']=0;					
				$check = false;					
				}else
				{
				   if($productcheck[$key]['numcheck']>=$value['3'])
				   {
				     $productcheck[$key]['numcheck']=$value['3'];				   
				   }else
				   {
				     $productcheck[$key]['numcheck']=0;	
					 $numcheck = false;				   
				   }			
				}				
			}
		}
		$return_data['buy'] = $buyarray;
		$return_data['product'] = $productdata;
		$return_data['productcheck'] = $productcheck;		
		$return_data['check'] = $check;	
		$return_data['numcheck'] = $numcheck;
		$return_data['shopmoney'] = $shopmoney;	
		$return_data['totalmoney'] = $totalmoney;							
		return $return_data;		
	}

    function buyidstr_to_array($buyidstr)
	{
        $detail=del_empty_var(explode(",",$buyidstr));
		$buyarray = array();
        foreach( $detail AS $key=>$value)
		{
		   $temp_data = explode('_', $value);
		   if(!isset($temp_data['0']))$temp_data['0']=0;
		   if(!isset($temp_data['1']))$temp_data['1']=0;
		   if(!isset($temp_data['2']))$temp_data['2']=0;
		   if(!isset($temp_data['3']))$temp_data['3']=1;		   						   
	       if(intval($temp_data['2'])>0||intval($temp_data['0'])>0)
		   {
		      $key_str = $temp_data['0'].'_'.$temp_data['1'].'_'.$temp_data['2'];
              $buyarray[$key_str]['0']=$temp_data[0];
              $buyarray[$key_str]['1']=$temp_data[1];			  
              $buyarray[$key_str]['2']=$temp_data[2];
              $buyarray[$key_str]['3']=$temp_data[3];			  			  
	       }
        }
		return $buyarray;		
	}

    function deleteorder()
    {
	     $id = $_GET['id'];
		 $TB_order = &$this->_model('do'); 	 
			 
		 $TB_order->del($id);	
		  
	    $notice = &get_message(array('msg'=>'操作成功','url'=>'?app=do&act=order','title'=>'Notice'));
	    $notice ->display();  

    }

	function confirmorder()
	{
		$id=$_GET['id'];	 	
		$TB_order = &$this->_model('do');  				
		$order = $TB_order->ordercontent($id);
        $this->assign('order', $order);
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');			
        $this->display('do_order_confirm.view.html');
	}

	function doconfirmorder()
	{
        $type=$_POST['type'];
        $money=$_POST['money'];
        $number=$_POST['number'];
        $orderid=$_POST['orderid'];

		if($type=='' || $number=='')
		{
		 $notice = &get_message(array('msg'=>'您必须详细记录所有交易信息!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}

		if($money<0)
		{
		 $notice = &get_message(array('msg'=>'错误，交易金额必须是一个大于等于0的整数!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}			

		$money=intval($money);

	    $db = & get_db();		
        $order = $db->get_one("SELECT * FROM `".DB_PREFIX."shoporderuser` WHERE id = '{$orderid}' and status = 1  LIMIT 1");	
	    $buyidstr=$order['productmsg'];
		if($order['ifpay']==1)
		{
		 $notice = &get_message(array('msg'=>'错误，该订单已经支付过了!','url'=>'','title'=>'Notice'));
		 $notice ->display();	
		}			
		$TB_order = &$this->_model('do'); 
		$TB_order->moneyadd($money,$order['uid']);

		$ary=array(
			'userid' => $order['uid'],
			'type' => 1,
			'name' => '充值(后台)',
			'intro' => '您充值了'.$money.'元，由管理员（'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'）确认收到货款',
			'money' => $money,
			'time' => time(),
		);

		$TB_order->moneylog($ary);

	      $totalmoney=0;
	      $shopmoney=0;/*统计积分数*/
          $getdata = $this->buyidstr_to_product_msg($order['productmsg'],1);
          if(!$getdata['check'])
		  {
		    $notice = &get_message(array('msg'=>'购买的产品当中，有一个产品已经不存在了','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }	
          if(!$getdata['numcheck'])
		  {
		    $notice = &get_message(array('msg'=>'购买的产品当中，有一个产品的库存不足','url'=>'?','title'=>'Notice'));
		    $notice ->display();		  
		  }			  	
	   	  $buyarray = $getdata['buy'];
		  $productdata = $getdata['product'];
		  $shopmoney = $getdata['shopmoney'];		  
		  $totalmoney = $getdata['totalmoney'];
		  $totalmoney_1= $totalmoney;		
	      if($order['sendtype']=='EMS快递')
		  {
		  $totalmoney+=25;
	      }
		  elseif($order['sendtype']=='其他快递')
		  {
		  $totalmoney+=18;
	      }elseif($order['sendtype']=='平邮')
		  {
		  $totalmoney+=8;
	      }
		  $time = time();
          $uid = $order['uid'];
		  $usermoney = $db->get_one("SELECT money FROM `".DB_PREFIX."users` WHERE user_id = '{$uid}' LIMIT 1");
		  $usermoney = $usermoney['money']; 	
	      if($usermoney>=$totalmoney)
		  {
	            $pay = $TB_order->moneypay($totalmoney,$uid,$shopmoney);
		        foreach($productdata as $key => $value)
		        {
                    foreach($value as $key1 => $value1)
			        {
				       $key_str = $key.'_'.$value1['cat_id'].'_'.$value1['id'];
				       if($value1['shopnum']>0)
				       {
				       $db->query("UPDATE `".DB_PREFIX."content_".$key."` SET `shopnum`=`shopnum`-".$buyarray[$key_str]['3']." WHERE id='".$value1['id']."'");				
				       }							    
			        } 		
		       }
 	           $db->query("UPDATE `".DB_PREFIX."shoporderuser` SET ifpay=1,paytime={$time} WHERE id='$orderid'");			    				
               $ary=array(
				   'userid' => $uid,
				   'type' => 0,
				   'name' => '由管理员（'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'）确认支付',
				   'intro' => '购买商'.'由管理员（'.$GLOBALS['SCup']->SCup_user_msg['data']['user_name'].'）确认支付'.',支付总金额'.$totalmoney.'元（商品总价格：'.$totalmoney_1.'，运输方式：'.$order['sendtype'].'，运输费用：'.($totalmoney-$totalmoney_1).'），订单号是'.$orderid,
				   'money' => $totalmoney,
				   'time' => time(),
		        );
		        $TB_order->moneylog($ary);
				$this->doqueue($order['uid'],$order['username'],$getdata,$order['productmsg'],$totalmoney_1,$totalmoney-$totalmoney_1,$orderid);
		        $notice = &get_message(array('msg'=>'操作成功！','url'=>'?','title'=>'Notice'));
		        $notice ->display();	
	     }else
		 {
		      $notice = &get_message(array('msg'=>'账上余额为'.$usermoney.'元，余额不足','url'=>'?','title'=>'Notice'));
		      $notice ->display();	
	     }
	}

	function doqueue($userid=0,$username='',$getdata=array(),$productmsg = '',$productmoney=0,$sendmoney=0,$orderid=0)
	{
		$productdata = $getdata['product'];
	    $db = & get_db();
		$t_queuetypedata = $db->get_all("SELECT id,table_id,cat_id,productid,queuemoney FROM `".DB_PREFIX."queuetype` WHERE table_id>0 ");	
		$queuetypedata = array();
		$queuetypeid_data = array();
		foreach($t_queuetypedata as $key => $value)
		{
		     $queuetypedata[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['queuemoney'];
			 $queuetypeid_data[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['id'];
		}
        $tongji = array();
		$queuemoneydata = array();
		foreach($productdata as $key => $value)
		{
             foreach($value as $key1 => $value1)
			 {
			      $queuemoney = 100 ;
				  $icon = 'default';
				  $queueid = 0  ;
				  if(isset($queuetypedata['0_0_0']))
				  {
					 $queuemoney = $queuetypedata['0_0_0']; 
					 $queueid = $queuetypeid_data['0_0_0'];
					 $icon = 'all'; 
				  }				  
				  if(isset($queuetypedata[$key.'_0_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_0_0']; 
					 $queueid = $queuetypeid_data[$key.'_0_0'];
					 $icon = 'table'.$key;  
				  }
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_0']; 
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_0'];
					 $icon = 'cat'.$key.'_'.$value1['cat_id'];
				  }				  				    
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']]))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $icon = 'product'.$key.'_'.$value1['cat_id'].'_'.$value1['id'];
				  }
				  $queuemoneydata[$icon] = $queuemoney;
				  $queueiddata[$icon] = $queueid;
				  $tongji[$icon][$key1]['data'] = $value1;
				  $tongji[$icon][$key1]['buynum'] = $getdata['buy'][$key.'_'.$value1['cat_id'].'_'.$value1['id']][3];
				  $tongji[$icon][$key1]['money'] = $value1['nowprice'];
				  $tongji[$icon][$key1]['tableid'] = $key;
				  $tongji[$icon][$key1]['cat_id'] = $value1['cat_id'];
				  $tongji[$icon][$key1]['id'] = $value1['id'];
				  	    
			 } 		
		}
       $sql = 'insert into '.DB_PREFIX.'queuemsg(uid,username,table_id,cat_id,productid,queuemoney,add_time,paytime,productmsg,totalmoney,onemoney,buynum,orderid,queueid) VALUES';
        $time = time();	
		$start = 0 ;	
		foreach($tongji as $key => $value)
		{
		    $kkmoneytotal =0;
			$tb = 0 ;
			$ct = 0 ;
			$pi = 0 ;
			$buynum = 0 ;
			$queuemoney = $queuemoneydata[$key];
			$queueid = $queueiddata[$key];
		    foreach($value as $key1 => $value1)
			{
			  $kkmoneytotal += $value1['money'] * $value1['buynum'] ;
			  $buynum += $value1['buynum'] ;
			  $tb = $value1['tableid'] ;
			  $ct = $value1['cat_id'] ;
			  $pi = $value1['id'] ;			  
			}
		    $kk = intval($kkmoneytotal / $queuemoney);

			if($kk >=1)
			{
			    for($i= 0 ; $i < $kk ;$i++)
			    {
				   $time = $time + $i ;
			      $sql .= "('{$userid}','{$username}','{$tb}','{$ct}','{$pi}','{$queuemoney}','{$time}','{$time}','{$productmsg}','{$productmoney}','{$kkmoneytotal}','{$buynum}','{$orderid}','{$queueid}'),";   
			    }			
			    $start = 1;
			}		 
		}
		if($start)
		{
		    $sql = substr($sql, 0,-1) ;
		    if($db->query($sql))
		    {
		     return 1 ;
		    }
			return 0 ;			
		}
		return 'no';
	}
	function doqueue_1($userid=0,$username='',$getdata=array(),$productmsg = '',$productmoney=0,$sendmoney=0,$orderid=0)
	{			onethread();
		$queue_num = QUEUENUM ;	
		if(empty($queue_num) || $queue_num<1)return 0 ;	
		$productdata = $getdata['product'];
	    $db = & get_db();
		$t_queuetypedata = $db->get_all("SELECT id,table_id,cat_id,productid,queuemoney FROM `".DB_PREFIX."queuetype` WHERE table_id>0 ");	
		$queuetypedata = array();
		$queuetypeid_data = array();
		foreach($t_queuetypedata as $key => $value)
		{
		     $queuetypedata[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['queuemoney'];
			 $queuetypeid_data[$value['table_id'].'_'.$value['cat_id'].'_'.$value['productid']] = $value['id'];
		}
        $tongji = array();
		$queuemoneydata = array();
		foreach($productdata as $key => $value)
		{
             foreach($value as $key1 => $value1)
			 {
			      $queuemoney = 100 ;
				  $icon = 'default';
				  $queueid = 0  ;
				  if(isset($queuetypedata['0_0_0']))
				  {
					 $queuemoney = $queuetypedata['0_0_0']; 
					 $queueid = $queuetypeid_data['0_0_0'];
					 $icon = 'all'; 
				  }				  
				  if(isset($queuetypedata[$key.'_0_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_0_0']; 
					 $queueid = $queuetypeid_data[$key.'_0_0'];
					 $icon = 'table'.$key;  
				  }
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_0']))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_0']; 
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_0'];
					 $icon = 'cat'.$key.'_'.$value1['cat_id'];
				  }				  				    
				  if(isset($queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']]))
				  {
					 $queuemoney = $queuetypedata[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $queueid = $queuetypeid_data[$key.'_'.$value1['cat_id'].'_'.$value1['id']];
					 $icon = 'product'.$key.'_'.$value1['cat_id'].'_'.$value1['id'];
				  }
				  $queuemoneydata[$icon] = $queuemoney;
				  $queueiddata[$icon] = $queueid;
				  $tongji[$icon][$key1]['data'] = $value1;
				  $tongji[$icon][$key1]['buynum'] = $getdata['buy'][$key.'_'.$value1['cat_id'].'_'.$value1['id']][3];
				  $tongji[$icon][$key1]['money'] = $value1['nowprice'];
				  $tongji[$icon][$key1]['tableid'] = $key;
				  $tongji[$icon][$key1]['cat_id'] = $value1['cat_id'];
				  $tongji[$icon][$key1]['id'] = $value1['id'];
				  	    
			 } 		
		}
       $sql = 'insert into '.DB_PREFIX.'queuemsg_1(uid,username,table_id,cat_id,productid,queuemoney,add_time,paytime,productmsg,totalmoney,onemoney,buynum,orderid,queueid,isqueue,isyushu,isyouhui) VALUES';
        $time = time();	
		$start = 0 ;	
		foreach($tongji as $key => $value)
		{
		    $kkmoneytotal =0;
			$tb = 0 ;
			$ct = 0 ;
			$pi = 0 ;
			$buynum = 0 ;
			$queuemoney = $queuemoneydata[$key];
			$queueid = $queueiddata[$key];
		    foreach($value as $key1 => $value1)
			{
			  $kkmoneytotal += $value1['money'] * $value1['buynum'] ;
			  $buynum += $value1['buynum'] ;
			  $tb = $value1['tableid'] ;
			  $ct = $value1['cat_id'] ;
			  $pi = $value1['id'] ;			  
			}
		    $kk = intval($kkmoneytotal / $queuemoney);			
            $youhui_num = intval($kk / $queue_num );
			$yushu_num = $kk % $queue_num;
			$isqueue = $isyushu = $isyouhui = 0 ;
			$youhui_array = $yushu_array = array();	
			if($youhui_num)
			{
				$youhui_array[1] = 1 ;
				if($youhui_num>1)
				{  $ka = 1 ;
			       for($i= 1 ; $i < $youhui_num ;$i++)
			       {
				     $ka = $ka + $queue_num ;
					$youhui_array[$ka] = $ka ;
			       }						
				}
			}
			if($yushu_num)
			{
			    for($i= 0 ; $i < $yushu_num ;$i++)
			    {
					$yushu_array[$kk - $i] = $kk - $i ;
			    }						
			}
		
			if($kk >=1)
			{
			    for($i= 1 ; $i <= $kk ;$i++)
			    {
				   $isqueue = $isyushu = $isyouhui = 0 ;
				   $time = $time + $i ;
				   if($youhui_num && isset($youhui_array[$i]) && !empty($youhui_array[$i]) )
				   {
					  $isqueue = 1; 
					  $isyouhui = 1;
				   }
				   if($yushu_num && isset($yushu_array[$i]) && !empty($yushu_array[$i]) )
				   {
					  $isyushu = 1; 
				   }				   
			       $sql .= "('{$userid}','{$username}','{$tb}','{$ct}','{$pi}','{$queuemoney}','{$time}','{$time}','{$productmsg}','{$productmoney}','{$kkmoneytotal}','{$buynum}','{$orderid}','{$queueid}','{$isqueue}','{$isyushu}','{$isyouhui}'),";  
				    
			    }			
			    $start = 1;
			}		 
		}
		if($start)
		{
		    $sql = substr($sql, 0,-1) ;
		    if($db->query($sql))
		    {
			  $start1 = 0;	
		      foreach($tongji as $key => $value)
		      {	
			   $queueid = $queueiddata[$key];			  			
		       $yushu_cha_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isyushu=1 && isqueue=0 && queueid={$queueid}  ORDER BY add_time asc ");	
			   $isdayutotal = $db->get_one("SELECT COUNT(*) AS num  FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ");
		       $isdayutotal=$isdayutotal['num'];
               $total_num_isyushu = count($yushu_cha_data);
			   $num = intval($total_num_isyushu / ($queue_num-1)) ;
			   $quxiaoyushu = array();
			   $queue_num_1 = $queue_num;
			   if($isdayutotal>=$queue_num && $num)
			   {
				   if($isdayutotal==$queue_num)
				   {
					  $queue_num_1 = $queue_num + 1; 
				   }
				   $i = 0 ;
				   $sql_str = $sql_str1 = ''; 
				   foreach($yushu_cha_data as $key => $value)
				   {
					  $i++ ;
					  if($i<= ($num*($queue_num_1-1) ))
					  {
					    $quxiaoyushu[$value['id']] = $value['id'];
						 $sql_str.="id='".$value['id']."'  or ";
					  }else
					  {
					   break ;
					  }  
				   }
				   $sql_str = substr($sql_str, 0,-3);
		           $queue_is_data = $db->get_all("SELECT id FROM `".DB_PREFIX."queuemsg_1` WHERE isqueue=0  && queueid={$queueid} ORDER BY add_time asc  LIMIT 0,{$num} ");	
				   foreach($queue_is_data as $key => $value)
				   {
					  $sql_str1.="id='".$value['id']."'  or ";
				   }	
				   $sql_str1 = substr($sql_str1, 0,-3);	
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isyushu=0 WHERE ".$sql_str." ");
		           $db->query("UPDATE ".DB_PREFIX."queuemsg_1 SET isqueue=1 WHERE ".$sql_str1." ");
				   $start1 = 1 ;			
			   }
			  }
			  if($start1)return 1 ;
		    }
			return 0 ;			
		}
		return 'no';
	}
   
}




?>